package ms.unicorn.dsl.runtime;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

import ms.unicorn.dsl.utils.SphynxRuntimeUtil;

public class SphynxRuntime {
	
	public static final Object NA = new Object() {
		public String toString() { return "NA"; }
	};
	
	public static void stop() {
		return;
	}
	
	public static int length(Object s) {
		if (s instanceof String)
			return ((String) s).length();
		if (s != null && s.getClass().isArray())
			return Array.getLength(s);
		return 0;
	}
	
	public static boolean isNA(Object obj) {
		return obj == NA;
	}
	
	public static boolean isList(Object obj) {
		return obj instanceof List;
	}
	
	public static boolean isMap(Object obj) {
		return obj instanceof Map;
	}
	
	public static Integer asInt(Object obj) {
		return (Integer) (obj instanceof Integer ? obj : obj != null ? Integer.parseInt(obj.toString()) : 0);
	}
	
	public static boolean isNullOrBlank(String s) {
		return s == null || s.trim().length() == 0;
	}
	
	public static boolean isNullOfEmtpy(String s) {
		return s == null || s.length() == 0;
	}
	
	public static boolean instanceOf(Object obj, Object clazz) {
		return clazz instanceof Class && ((Class<?>)clazz).isInstance(obj);
	}
	
	public static final Map<String, SimpleDateFormat> dateformats = new HashMap<String, SimpleDateFormat>();  
	
	public static Date asDate(Object s, String format) throws ParseException {
		if (s instanceof String) {
			SimpleDateFormat sdf = dateformats.get(format);
			if (sdf == null)
				dateformats.put(format, sdf = new SimpleDateFormat(format));
			return sdf.parse((String)s);
		} else if (s instanceof Integer) {
			return new Date((Long)s);
		}
		return null;
	}
	
	public static String formatDate(Date d, String format) {
		SimpleDateFormat sdf = dateformats.get(format);
		if (sdf == null)
			dateformats.put(format, sdf = new SimpleDateFormat(format));
		return sdf.format(d);
	}
	
	public static Pattern compilePattern(String pattern, int flag) {
		return SphynxRuntimeUtil.compilePattern(pattern, flag);
	}
	
	
	public static int exec2(String outputFile, Object... args) {		
		try {
			String[] cmds = new String[args.length];
			for (int i = 0; i < args.length; i++)
				cmds[i] = args[i].toString();
			
			Process p = Runtime.getRuntime().exec(cmds);
			byte[] buffer = new byte[10*1024];
			int bread;
			
			InputStream istream = p.getInputStream();
			OutputStream stdout = System.out;
			if (outputFile != null && outputFile.length() > 0)
				stdout = new FileOutputStream(new File(outputFile));
			while ((bread = istream.read(buffer)) > 0)
				stdout.write(buffer, 0, bread);
			
			istream.close();
			if (stdout != System.out)
				stdout.close();
			
			return p.waitFor();
		} catch (Exception e) {
			throw new RuntimeException("Error while executing command", e);
		}
		
	}
	
	public static int exec(String cmds, String outputFile) {		
		try {
			Process p = Runtime.getRuntime().exec(cmds);
			byte[] buffer = new byte[10*1024];
			int bread;
			
			InputStream istream = p.getInputStream();
			OutputStream stdout = System.out;
			if (outputFile != null && outputFile.length() > 0)
				stdout = new FileOutputStream(new File(outputFile));
			while ((bread = istream.read(buffer)) > 0)
				stdout.write(buffer, 0, bread);
			
			istream.close();
			if (stdout != System.out)
				stdout.close();
			
			return p.waitFor();
		} catch (Exception e) {
			throw new RuntimeException("Error while executing command " + cmds, e);
		}
		
	}
	
	public Image loadImage(String path)  {
		try {
			return ImageDescriptor.createFromURL(new URL("file:" + path)).createImage();
		} catch (MalformedURLException e) {
			return null;
		}
	}
}
